package ru.lukyanets.transmission.client.action;

import com.google.inject.Inject;
import javafx.event.ActionEvent;
import javafx.scene.control.MenuItem;
import ru.lukyanets.transmission.client.config.ConnectionConfig;
import ru.lukyanets.transmission.client.connection.ConnectionInfo;
import ru.lukyanets.transmission.client.connection.TransmissionService;

import com.google.inject.Singleton;

/**
 * @author slukyanets
 */
@Singleton
public class ConnectAction extends AbstractAction {
    /** @noinspection unused*/
    @Inject
    private TransmissionService transmissionService;

    @Inject
    ConnectionConfig config;

    @Override
    protected void performAction(ActionEvent event) {
        if(event.getSource() != null && event.getSource().getClass().equals(MenuItem.class)) {
            ConnectionInfo info = (ConnectionInfo) ((MenuItem)event.getSource()).getUserData();
            transmissionService.connect(info);
            config.setDefault(info.getName());
            config.store();
        } else if (config.getDefaultConnection() != null) {
            transmissionService.connect(config.getDefaultConnection());
        }
    }
}
